// 路由配置
import { createRouter, createWebHistory } from 'vue-router';

const routes = [
  {
    path: '/',
    name: 'Layout',
    component: () => import('../views/Layout.vue'),
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('../views/Home.vue'),
        meta: { title: '首页', requiresAuth: false }
      },
      {
        path: 'booking',
        name: 'Booking',
        component: () => import('../views/Booking.vue'),
        meta: { title: '预订台球桌', requiresAuth: true }
      },
      {
        path: 'orders',
        name: 'Orders',
        component: () => import('../views/MyOrders.vue'),
        meta: { title: '我的订单', requiresAuth: true }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('../views/Profile.vue'),
        meta: { title: '个人中心', requiresAuth: true }
      },
      {
        path: 'recharge',
        name: 'Recharge',
        component: () => import('../views/Recharge.vue'),
        meta: { title: '账户充值', requiresAuth: true }
      },
      {
        path: 'announcement/:id',
        name: 'Announcement',
        component: () => import('../views/Announcement.vue'),
        meta: { title: '公告详情', requiresAuth: false }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: { title: '注册', requiresAuth: false }
  },
  {
    path: '/announcement/:id',
    name: 'AnnouncementDetail',
    component: () => import('../views/AnnouncementDetail.vue'),
    meta: { title: '公告详情', requiresAuth: false }
  },
  // 管理端路由
  {
    path: '/admin',
    name: 'AdminLayout',
    component: () => import('../views/admin/Layout.vue'),
    redirect: '/admin/users',
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/admin/Dashboard.vue'),
        meta: { title: '控制台', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'tables',
        name: 'Tables',
        component: () => import('../views/admin/Tables.vue'),
        meta: { title: '台球桌管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'users',
        name: 'UserManagement',
        component: () => import('../views/admin/UserManagement.vue'),
        meta: { title: '用户管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'orders',
        name: 'AdminOrders',
        component: () => import('../views/admin/Orders.vue'),
        meta: { title: '订单管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'inventory',
        name: 'Inventory',
        component: () => import('../views/admin/Inventory.vue'),
        meta: { title: '库存管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'reports',
        name: 'Reports',
        component: () => import('../views/admin/Reports.vue'),
        meta: { title: '财务报表', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'activities',
        name: 'Activities',
        component: () => import('../views/admin/Activities.vue'),
        meta: { title: '活动公告', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'settings',
        name: 'Settings',
        component: () => import('../views/admin/Settings.vue'),
        meta: { title: '系统设置', requiresAuth: true, requiresAdmin: true }
      }
    ]
  },

  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue'),
    meta: { title: '页面不存在', requiresAuth: false }
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 一杆云枢` : '一杆云枢';

  // 获取本地存储的 token 和用户信息
  const token = localStorage.getItem('token');
  const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');

  // 需要登录但未登录
  if(to.meta.requiresAuth && !token){
    next({ name: 'Login', query: { redirect: to.fullPath } });
    return;
  }

  // 需要管理员权限但不是管理员
  if (to.meta.requiresAdmin && userInfo.role !== 'admin') {
    next({ name: 'Home' });
    return;
  }
  
  next();
});

export default router;